7326. Sleeping cars

 

A train contains sleeping cars, marked with letter k, and sitting cars, marked with letter p. Find the biggest number of sleeping cars that follow each other in the train.

 

Input. One line contains a sequence of letters k and p, with length from 1 to 1000 characters.

 

Output. One line contains a sequence of letters k and p, with length from 1 to 1000 characters.

 

Sample input

Sample output

kpkkp

2

 

 

SOLUTION

string

 

Algorithm analysis

In the given string, you need to find the longest substring consisting only of the letters k.

Lets declare the variable temp – a counter of consecutive letters k. If the current letter is k, then increase temp by 1. Otherwise, reset temp to 0. The maximum reachable value temp equals to the answer, count it in the res variable.

 

Example

Consider the given example.

 

Algorithm realization

Store the input string in the array s.

 

char s[1010];

 

Read the input string s.

 

gets(s);

 

In the variable temp count the number of consecutive letters k. Store the answer in the variable res.

 

res = temp = 0;

 

Iterate over the letters of the string s.

 

for (i = 0; i < strlen(s); i++)

{

 

If the current letter is k, then increase temp by 1. Otherwise, the sequence of consecutive letters k is cut off, set temp to 0.

 

  if (s[i] == 'k') temp++; else temp = 0;

 

The answer is the maximum among all possible values of temp.

 

  if (temp > res) res = temp;

}

 

Print the answer.

 

printf("%d\n",res);

 

Algorithm realization string

Read the input string s.

 

cin >> s;

 

In the variable temp count the number of consecutive letters k. Store the answer in the variable res.

 

res = temp = 0;

 

Iterate over the letters of the string s.

 

for (i = 0; i < s.size(); i++)

{

 

If the current letter is k, then increase temp by 1. Otherwise, the sequence of consecutive letters k is cut off, set temp to 0.

 

  if (s[i] == 'k') temp++; else temp = 0;

 

The answer is the maximum among all possible values of temp.

 

  if (temp > res) res = temp;

}

 

Print the answer.

 

cout << res << endl;

 

Algorithm realization formatted input

 

#include <cstdio>

#include <cstring>

 

char s[1010];

int res, len;

 

int main()

{

  res = 0;

  scanf("%[p]",s);

  while(scanf("%[k]",s) == 1)

  {

    len = strlen(s);

    if (len > res) res = len;

    scanf("%[p]",s);

  }

  printf("%d\n",res);

  return 0;

}

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String []args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();

    int res = 0, temp = 0;

    for (int i = 0; i < s.length(); i++)

    {

      if (s.charAt(i) == 'k') temp++; else temp = 0;

      if (temp > res) res = temp;

    }

    System.out.println(res);

    con.close();

  }

}   

 

Python realization

Read the input string s.

 

s = input()

 

In the variable c count the number of consecutive letters k. Store the answer in the variable res.

 

c = res = 0

 

Iterate over the letters of the string s.

 

for x in s:

 

If the current letter is k, then increase c by 1. Otherwise, the sequence of consecutive letters k is cut off, set c to 0.

 

  if x == 'k':

    c += 1

  else: c = 0

 

The answer is the maximum among all possible values of temp.

 

  res = max(res, c)

 

Print the answer.

 

print(res)